package counter.com.baomidou.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import counter.com.baomidou.entity.Methodinfo;
import counter.com.baomidou.entity.Serverinfo;
import counter.com.baomidou.service.IMethodinfoService;
import counter.com.common.Result;
import counter.com.connproperty.methodinfo.MethodAddProperty;
import counter.com.connproperty.methodinfo.MethodDeleteProperty;
import counter.com.connproperty.methodinfo.MethodSearchProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-10-01
 */
@RestController
@RequestMapping("/methodinfo")
public class MethodinfoController {
    @Autowired
    IMethodinfoService service;
    private static int PASSCHECK = 0;

    @PostMapping("/search")
    public Result search(@RequestBody MethodSearchProperty data) {
        LambdaQueryWrapper<Methodinfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Methodinfo::getId);
        Page<Methodinfo> page = new Page<>(data.getPage(), data.getPagesize());
        IPage<Methodinfo> iPage = null;
        if (data.getType() != PASSCHECK) {
            wrapper.eq(Methodinfo::getType, data.getType());
        }
        iPage = service.page(page, wrapper);
        return Result.success(iPage);
    }

    @PostMapping("/add")
    public Result add(@RequestBody MethodAddProperty data) {
        Methodinfo methodinfo = new Methodinfo();
        methodinfo.setName(data.getName());
        methodinfo.setType(data.getType());
        methodinfo.setDocpath(data.getDocpath());
        methodinfo.setVideopath(data.getVideopath());
        methodinfo.setVm(data.getVm());
        methodinfo.setUpdatetime(LocalDateTime.now());
        methodinfo.setDocname(data.getDocname());
        methodinfo.setVideoname(data.getVideoname());
        if (service.save(methodinfo)) {
            return Result.success("add success");
        } else {
            return Result.fail("add failed");
        }
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody MethodDeleteProperty data) {
        if (service.removeByIds(Arrays.asList(data.getIds()))) {
            return Result.success("delete success");
        } else {
            return Result.fail("delete failed");
        }
    }
}
